/*
 * MainTimer.c
 *
 *  Created on: 30.01.2011
 *      Author: Tavin
 */




#include <stdint.h>

#include "board.h"


#include "intc.h"
#include "tc.h"

#include "MainTimer.h"

#include "Random.h"
#include "Display2D.h"


volatile avr32_tc_t *mainTc = &AVR32_TC;



#if __GNUC__
__attribute__((__interrupt__))
#elif __ICCAVR32__
#pragma handler = AVR32_PDCA_IRQ_GROUP, 0
__interrupt
#endif
static void MainTimer_irq(void)
{


  // Clear the interrupt flag. This is a side effect of reading the TC SR.
  tc_read_sr(mainTc, MAIN_TC_CHANNEL_ID);
  mainTimer.systemTime++;
  // TODO if Tick still true -> something is wrong !!!!
  MainTimer_setTick();

  Random_setValue(Random_getValue()+mainTimer.systemTime);

  Display2D_refresh();

}





void MainTimer_init()
{
	static const tc_waveform_opt_t WAVEFORM_OPT =
		  {
		    .channel  = MAIN_TC_CHANNEL_ID,                        // Channel selection.

		    .bswtrg   = TC_EVT_EFFECT_NOOP,                // Software trigger effect on TIOB.
		    .beevt    = TC_EVT_EFFECT_NOOP,                // External event effect on TIOB.
		    .bcpc     = TC_EVT_EFFECT_NOOP,                // RC compare effect on TIOB.
		    .bcpb     = TC_EVT_EFFECT_NOOP,                // RB compare effect on TIOB.

		    .aswtrg   = TC_EVT_EFFECT_NOOP,                // Software trigger effect on TIOA.
		    .aeevt    = TC_EVT_EFFECT_NOOP,                // External event effect on TIOA.
		    .acpc     = TC_EVT_EFFECT_NOOP,                // RC compare effect on TIOA: toggle.
		    .acpa     = TC_EVT_EFFECT_NOOP,                // RA compare effect on TIOA: toggle (other possibilities are none, set and clear).

		    .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,// Waveform selection: Up mode with automatic trigger(reset) on RC compare.
		    .enetrg   = FALSE,                             // External event trigger enable.
		    .eevt     = 0,                                 // External event selection.
		    .eevtedg  = TC_SEL_NO_EDGE,                    // External event edge selection.
		    .cpcdis   = FALSE,                             // Counter disable when RC compare.
		    .cpcstop  = FALSE,                             // Counter clock stopped with RC compare.

		    .burst    = FALSE,                             // Burst signal selection.
		    .clki     = FALSE,                             // Clock inversion.
		    .tcclks   = TC_CLOCK_SOURCE_TC3                // Internal source clock 3, connected to fPBA / 4.
		  };

	static const tc_interrupt_t TC_INTERRUPT =
		  {
		    .etrgs = 0,
		    .ldrbs = 0,
		    .ldras = 0,
		    .cpcs  = 1,
		    .cpbs  = 0,
		    .cpas  = 0,
		    .lovrs = 0,
		    .covfs = 0
		  };


	mainTimer.systemTime=0;

	tc_init_waveform(mainTc, &WAVEFORM_OPT);

	tc_write_rc(mainTc, MAIN_TC_CHANNEL_ID, 3500); // Set RC value.
	tc_configure_interrupts(mainTc, MAIN_TC_CHANNEL_ID, &TC_INTERRUPT);
	tc_start(mainTc, MAIN_TC_CHANNEL_ID);


	Disable_global_interrupt();
	INTC_register_interrupt((__int_handler)&MainTimer_irq, AVR32_TC_IRQ0, AVR32_INTC_INT0);
	Enable_global_interrupt();






}

/*
void MainTimer_setFlag(uint8_t flag)
{
	mainTimer.status|=flag;
}


void MainTimer_SetFlag(uint8_t flag)
{
	mainTimer.status|=flag;
}
*/

